<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="informationTitle">
        <el-input v-model="queryParams.informationTitle" placeholder="请输入标题" clearable size="small"
                  maxlength="30"
                  show-word-limit
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="发布人" prop="informationAuthor">
        <el-input v-model="queryParams.informationAuthor" placeholder="请输入发布人" clearable size="small"
                  maxlength="30"
                  show-word-limit
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否显示" prop="isDisplay">
        <el-select v-model="queryParams.isDisplay" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in displayOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-select v-model="queryParams.isTop" placeholder="请选择" clearable>
          <el-option
            v-for="dict in topOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="咨讯类型" prop="informationType">
        <el-select v-model="queryParams.informationType" placeholder="请选择咨讯类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" prop="dateRange">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['server:information:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['server:information:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['server:information:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index"  prop="informationId" key="informationId" v-if="columns[0].visible"/>
      <el-table-column label="标题" align="center" prop="informationTitle" :show-overflow-tooltip='true' key="informationTitle" v-if="columns[1].visible"/>
      <el-table-column label="内容" align="center" prop="informationDetails" :show-overflow-tooltip='true' key="informationDetails" v-if="columns[2].visible">
        <template slot-scope="scope" >
          <div style="height: 20px" v-html="scope.row.informationDetails"></div>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="informationAuthor" :show-overflow-tooltip='true' key="informationAuthor" v-if="columns[3].visible"/>
      <el-table-column label="是否显示" align="center" prop="isDisplay" :show-overflow-tooltip='true' key="isDisplay" v-if="columns[4].visible">
        <template slot-scope="scope">
          <span v-if="scope.row.isDisplay == 0">显示</span>
          <span v-if="scope.row.isDisplay == 1">不显示</span>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isTop" :show-overflow-tooltip='true' key="isTop" v-if="columns[5].visible">
        <template slot-scope="scope">
          <span v-if="scope.row.isTop == 1">置顶</span>
          <span v-if="scope.row.isTop == 2">不置顶</span>
        </template>
      </el-table-column>
      <el-table-column label="咨讯类型" align="center" prop="informationType" :formatter="informationTypeFormat" :show-overflow-tooltip='true' key="informationType" v-if="columns[6].visible"/>
      <el-table-column label="发布时间" align="center" prop="informationDatetime" width="180" :show-overflow-tooltip='true' key="informationDatetime" v-if="columns[7].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.informationDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip='true' key="remark" v-if="columns[8].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['server:information:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['server:information:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改运营管理资讯动态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="图片">
              <el-upload
                v-if="form.informationImg"
                ref="upload"
                action=""
                :show-file-list="false"
                list-type="picture-card"
                accept=".jpg,.jpeg,.png,.JPG,.JPEG"
                :auto-upload="false"
                :on-change="getFile">
                <img :src="form.informationImg" style="width: 148px;height: 148px;border-radius: 6px">
                <el-button slot="trigger" size="small" type="primary">选取图片</el-button>
                <div slot="tip" class="el-upload__tip">只能上传一张jpg/png图片，且不超过500kb</div>
              </el-upload>
              <el-upload
                v-else
                ref="upload"
                action=""
                :show-file-list="true"
                list-type="picture-card"
                accept=".jpg,.jpeg,.png,.JPG,.JPEG"
                :auto-upload="false"
                :on-change="getFile">
                <el-button slot="trigger" size="small" type="primary">选取图片</el-button>
                <div slot="tip" class="el-upload__tip">只能上传一张jpg/png图片，且不超过500kb</div>
              </el-upload>
            </el-form-item>
        <el-form-item label="标题" prop="informationTitle">
          <el-input v-model="form.informationTitle" placeholder="请输入标题" maxlength="200"/>
        </el-form-item>
        <el-form-item label="咨讯简介" prop="informationProfile">
          <el-input v-model="form.informationProfile" type="textarea"  placeholder="请输入简介" maxlength="500"></el-input>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="发布人" prop="informationAuthor">
          <el-input v-model="form.informationAuthor" placeholder="请输入发布人" maxlength="30"/>
        </el-form-item>
        <el-form-item label="是否显示" prop="isDisplay">
          <el-select v-model="form.isDisplay" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="dict in displayOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-select v-model="form.isTop" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="dict in topOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="咨讯类型" prop="informationType">
          <el-select v-model="form.informationType" placeholder="请选择咨讯类型" style="width: 100%;">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间" prop="informationDatetime">
          <el-date-picker clearable size="small" v-model="form.informationDatetime" type="date"
                          value-format="yyyy-MM-dd" placeholder="选择发布时间" style="width: 100%;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" maxlength="200"/>
        </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="内容" prop="informationDetails">
              <editor v-model="form.informationDetails" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInformation,
  getInformation,
  delInformation,
  addInformation,
  updateInformation,
  exportInformation
} from "@/api/server/operate/information";
import Editor from '@/components/Editor';

export default {
  name: "Information",
  components: {Editor},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运营管理资讯动态表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //类型字典
      typeOptions: [],
      //显示状态字典
      displayOptions: [],
      //置顶状态字典
      topOptions: [],
      //日期范围
      dateRange:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        informationDetails: null,
        informationTitle: null,
        informationAuthor: null,
        isDisplay: null,
        isTop: null,
        informationType: null,
        contentHit: null,
        dateRange:null,
      },
      // 表单参数
      form: {},
      //列信息
      columns: [
        {key: 0, label: "序号", visible: true},
        {key: 1, label: "标题", visible: true},
        {key: 2, label: "内容", visible: true},
        {key: 3, label: "发布人", visible: true},
        {key: 4, label: "是否显示", visible: true},
        {key: 5, label: "是否置顶", visible: true},
        {key: 6, label: "咨讯类型", visible: true},
        {key: 7, label: "发布时间", visible: true},
        {key: 8, label: "备注", visible: true},
      ],
      queryForm: {
        informationAuthor: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
      },
      // 表单校验
      rules: {
        informationTitle: [{
          required: true,
          message: "标题不能为空",
          trigger: "blur"
        }],
        informationProfile: [{
          required: true,
          message: "咨询简介不能为空",
          trigger: "blur"
        }],
        informationDetails: [{
          required: true,
          message: "内容不能为空",
          trigger: "blur"
        }],
        informationAuthor: [{
          required: true,
          message: "发布人不能为空",
          trigger: "blur"
        }],
        isDisplay: [{
          required: true,
          message: "是否显示不能为空",
          trigger: "change"
        }],
        isTop: [{
          required: true,
          message: "是否置顶不能为空",
          trigger: "change"
        }],
        informationType: [{
          required: true,
          message: "咨询类型不能为空",
          trigger: "change"
        }],
        informationDatetime: [{
          required: true,
          message: "发布时间不能为空",
          trigger: "blur"
        }],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('t_information_type').then(response => {
      this.typeOptions = response.data;
    })
    this.getDicts('t_is_display').then(response => {
      this.displayOptions = response.data;
    })
    this.getDicts('t_is_top').then(response => {
      this.topOptions = response.data;
    })
  },
  methods: {
    /** 查询运营管理资讯动态列表 */
    getList() {
      this.loading = true;
      listInformation(this.addDateRange1(this.queryParams, this.dateRange)).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //字典状态翻译
    informationTypeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.informationType)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        informationId: null,
        informationDetails: null,
        informationTitle: null,
        informationAuthor: null,
        isDisplay: null,
        isTop: null,
        informationType: null,
        informationDatetime: null,
        contentHit: null,
        createBy: null,
        delFlag: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs['queryForm'].validate(valid => {
        if (valid) {
          this.getList()
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams.params.beginTime = ''
      this.queryParams.params.endTime = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.informationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //上传图片
    getFile(file) {
      this.getBase64(file.raw).then(res => {
        this.form.informationImg = res
      });
    },
    //将本地图片转成base64
    getBase64(file) {
      return new Promise(function (resolve, reject) {
        let reader = new FileReader();
        let imgResult = "";
        reader.readAsDataURL(file);
        reader.onload = function () {
          imgResult = reader.result;
        };
        reader.onerror = function (error) {
          reject(error);
        };
        reader.onloadend = function () {
          resolve(imgResult);
        };
      });
    },
    /** 新增按钮操作} */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运营管理资讯动态";
      this.$nextTick(()=>{
        this.$refs.upload.clearFiles();
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const informationId =
        row.informationId || this.ids
      getInformation(informationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运营管理资讯动态";
        this.$nextTick(()=>{
          this.$refs.upload.clearFiles();
        })
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.informationId != null) {
            updateInformation(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const informationIds = row.informationId || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delInformation(informationIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有运营管理资讯动态数据项?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info"
      }).then(function () {
        return exportInformation(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
